package com.loctek.importexport.util;

import android.annotation.TargetApi;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.util.TypedValue;
import android.view.View;

import com.loctek.importexport.R;


public final class ViewHelpers {

    private ViewHelpers() {
    }

    static public void setViewBackground(View view, Drawable background) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN /* = 16 */)
            setViewBackgroundDrawable16AndUp(view, background);
        else
            setViewBackgroundDrawablePre16(view, background);
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    static private void setViewBackgroundDrawable16AndUp(View view, Drawable background) {
        view.setBackground(background);
    }

    @SuppressWarnings("deprecation")
    static private void setViewBackgroundDrawablePre16(View view, Drawable background) {
        view.setBackgroundDrawable(background);
    }

    /**
     * Method which returns the View representing the home button ("hamburger") on activities with an Action/Toolbar.
     * <p/>
     * TODO this doesn't seem to work (at least not on Lollipop) -> always returns null View
     *
     * @param activity
     * @return
     */
    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    static public View getActionBarHomeView(final Activity activity) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB && !(activity instanceof AppCompatActivity))
            return activity.findViewById(android.R.id.home);
        else
            return activity.findViewById(R.id.home);
    }

    /**
     * Returns, as pixels, the default/preferred padding on used on (Alert)Dialog contents
     *
     * @param activity
     * @return
     */
    static public int getDefaultDialogPaddingPx(Activity activity) {
        TypedValue typedValue = new TypedValue();
        activity.getTheme().resolveAttribute(android.R.attr.dialogPreferredPadding, typedValue, true);
        return (int) TypedValue.complexToDimensionPixelSize(typedValue.data, activity.getResources().getDisplayMetrics());
    }

}